package com.itheima.reggie.controller;

import cn.hutool.crypto.SmUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.entity.UserDto;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.util.SMSUtilsMarket;
import com.itheima.reggie.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * @author :XingJiaCheng
 * @date :2022-2022/12/25-11:51
 * @description
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
@PostMapping("/sendMsg")
    public R<String>sendMsg(@RequestBody User user, HttpSession session){
        log.info("获取验证码 user:{}", user);
    String phone = user.getPhone();
    String code = ValidateCodeUtils.generateValidateCode4String(4)+"";
    log.info("验证码:{}", code);
//    // 测试 以下签名、模板，appcode都是测试使用
    String smsSignId = "2e65b1bb3d054466b82f0c9d125465e2";  // 从购买的服务商获取
    String templateId = "908e94ccf08b4476ba6c876d13f084ad"; // 从购买的服务商获取
    String appCode = "77c2a05447644bb380aa119aefaa1b9d";     // 从自己的购买记录中获取
    SMSUtilsMarket.sendMessage(user.getPhone(),code+"",smsSignId,templateId,appCode);
    redisTemplate.opsForValue().set(phone, code, 6, TimeUnit.MINUTES);
        return R.success("获取验证码成功");
    }
    @PostMapping("/login")
    public R<String>login(@RequestBody UserDto userDto, HttpSession session) {
        log.info("验证码登录 dto:{},{}", userDto.getPhone(), userDto.getCode());
        Object codeInSession = redisTemplate.opsForValue().get(userDto.getPhone());
        log.info("redis:{}",codeInSession);
        if (codeInSession != null && codeInSession.toString().equals(userDto.getCode())) {
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getPhone, userDto.getPhone());
            User one = userService.getOne(lambdaQueryWrapper);
            if (one == null) {
                User user = new User();
                user.setPhone(userDto.getPhone());
                user.setStatus(1);
                userService.save(user);
                session.setAttribute("user", user.getId());
            } else {
                session.setAttribute("user", one.getId());
            }
            redisTemplate.delete(userDto.getPhone());
            return R.success("登录成功");
        }
        return R.error("登录失败,验证码无效过期");
    }
}
